package com.example.smartmallserver.demos.web.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.example.smartmallserver.demos.web.util.JwUtils;
import com.example.smartmallserver.demos.web.util.Result;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@Component
public class LoginCheckInterceptor implements HandlerInterceptor {

    //目标资源方法执行前执行。  return true 放行；return false 不放行；
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getMethod().equals("OPTIONS")){
            return true;
        }
        HttpSession session = request.getSession();;
        //获取url地址
        String url = request.getRequestURL().toString();
        System.out.println("!!!url:"+url);
        //判断请求地址是否为/login
        if (url.contains("login")){
            return true;
        }
        //判断请求地址是否为/captcha
        if (url.contains("captcha")){
            return true;
        }
        //判断请求地址是否为/page
        if (url.contains("page")){
            return true;
        }
        //判断请求地址是否为/goods
        if (url.contains("goods")){
            return true;
        }
        //判断请求地址是否为/comment
        if (url.contains("comment")){
            return true;
        }

        //获取token
        String jwt = request.getHeader("Access-Token");
        System.out.println("!!!token:"+jwt);
        //判断令牌是否存在
        if (!StringUtils.hasLength(jwt)){//令牌不存在
            Result error = Result.error("NOT_LOGIN");
            //手动转换对象为json对象
            String notLogin = JSONObject.toJSONString(error);
            response.getWriter().write(notLogin);
            System.out.println("!!!令牌不存在");
            return false;
        }

        //解析token
        try {
            Claims claims = JwUtils.ParseJwt(jwt);
            session.setAttribute("tel",claims.get("tel"));
            session.setAttribute("id",claims.get("id"));
            System.out.println("Claims:  "+claims);
        }
        catch (Exception e){//令牌解析失败
            Result error = Result.error("NOT_LOGIN");
            //手动转换对象为json对象
            String notLogin = JSONObject.toJSONString(error);
            response.getWriter().write(notLogin);
            System.out.println("!!!令牌解析失败");
            return false;
        }
        System.out.println("令牌解析成功，放行");
        return true;

    }

    //目标资源方法执行后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }

    //试图渲染完成后执行，最后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }
}
